package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.ResumeInfo;
import com.jyy.prefabricated.entity.ResumeInfoWorkHis;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ResumeInfoMapper;
import com.jyy.prefabricated.mapper.ResumeInfoWorkHisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */
@Service
@Transactional
public class ResumeInfoService {

    @Autowired
    private ResumeInfoMapper resumeInfoMapper;

    @Autowired
    private ResumeInfoWorkHisMapper resumeInfoWorkHisMapper;

    /**
     * 新增
     * @param resumeInfo
     * @return
     */
    public Integer insert(ResumeInfo resumeInfo){
        resumeInfoMapper.insert(resumeInfo);
        saveResumeId2WorkHis(resumeInfo);
        return resumeInfo.getId();
    }

    /**
     * 保存简历id到工作经历列表
     * @param resumeInfo
     */
    public void saveResumeId2WorkHis(ResumeInfo resumeInfo){
        List<ResumeInfoWorkHis> workHisList = resumeInfo.getWorkHis();
        if(workHisList.size() > 0){
            for(ResumeInfoWorkHis workHis : workHisList){
                workHis.setResumeInfoId(resumeInfo.getId());
            }
            resumeInfoWorkHisMapper.updateBatch(workHisList);
        }
    }

    /**
     * 更新
     * @param resumeInfo
     * @return
     */
    public int update(ResumeInfo resumeInfo){
        if(resumeInfo.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        saveResumeId2WorkHis(resumeInfo);
        return resumeInfoMapper.updateByPrimaryKeySelective(resumeInfo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        resumeInfoWorkHisMapper.deleteByResumeInfoId(id);
        return resumeInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ResumeInfo selectById(Integer id){
        return resumeInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询
     * @param keyword 关键字
     * @param residence 居住地
     * @param wantedJob 求职意向
     * @param userId 用户id
     * @param workYear 工作年份
     * @return
     */
    public List<ResumeInfo> selectByParams(String keyword,String residence, Integer wantedJob, Integer userId, Integer workYear){
        return resumeInfoMapper.selectByParams(keyword, residence, wantedJob, userId, workYear);
    }
}
